<script setup lang="ts">
import { computed, onMounted, ref } from 'vue';
import { createReusableTemplate } from '@vueuse/core';
import { fetchDashboardsCard } from '@/service/api/business/products';

defineOptions({
  name: 'CardData'
});

interface CardData {
  key: string;
  title: string;
  value: number;
  unit: string;
  color: {
    start: string;
    end: string;
  };
  icon: string;
}

// 定义响应式数据
const dashboardData = ref<Api.Business.DashboardCard | undefined>();

// 获取数据
const fetchData = async () => {
  try {
    const res = await fetchDashboardsCard();
    dashboardData.value = res.data;
  } catch (error) {
    console.error('获取仪表盘数据失败:', error);
  }
};

// 组件挂载时获取数据
onMounted(() => {
  fetchData();
});

// 定义默认数据
const DEFAULT_DASHBOARD_DATA = {
  agentTotal: 0,
  todayWithdraw: '0',
  todayTransaction: '0',
  todayOrders: 0
};

const cardData = computed<CardData[]>(() => {
  const data = dashboardData.value ?? DEFAULT_DASHBOARD_DATA;

  return [
    {
      key: 'agentTotal',
      title: '代理数量',
      value: data.agentTotal ?? 0,
      unit: '',
      color: {
        start: '#ec4786',
        end: '#b955a4'
      },
      icon: 'ant-design:bar-chart-outlined'
    },
    {
      key: 'todayWithdraw',
      title: '今日提现',
      value: Number(data.todayWithdraw ?? 0),
      unit: '$',
      color: {
        start: '#865ec0',
        end: '#5144b4'
      },
      icon: 'ant-design:money-collect-outlined'
    },
    {
      key: 'todayTransaction',
      title: '今日交易',
      value: Number(data.todayTransaction ?? 0),
      unit: '',
      color: {
        start: '#56cdf3',
        end: '#719de3'
      },
      icon: 'carbon:document-download'
    },
    {
      key: 'todayOrders',
      title: '今日订单',
      value: data.todayOrders ?? 0,
      unit: '',
      color: {
        start: '#fcbc25',
        end: '#f68057'
      },
      icon: 'ant-design:trademark-circle-outlined'
    }
  ];
});

interface GradientBgProps {
  gradientColor: string;
}

const [DefineGradientBg, GradientBg] = createReusableTemplate<GradientBgProps>();

function getGradientColor(color: CardData['color']) {
  return `linear-gradient(to bottom right, ${color.start}, ${color.end})`;
}
</script>

<template>
  <NCard :bordered="false" size="small" class="card-wrapper">
    <!-- define component start: GradientBg -->
    <DefineGradientBg v-slot="{ $slots, gradientColor }">
      <div class="rd-8px px-16px pb-4px pt-8px text-white" :style="{ backgroundImage: gradientColor }">
        <component :is="$slots.default" />
      </div>
    </DefineGradientBg>
    <!-- define component end: GradientBg -->

    <NGrid cols="s:1 m:2 l:4" responsive="screen" :x-gap="16" :y-gap="16">
      <NGi v-for="item in cardData" :key="item.key">
        <GradientBg :gradient-color="getGradientColor(item.color)" class="flex-1">
          <h3 class="text-16px">{{ item.title }}</h3>
          <div class="flex justify-between pt-12px">
            <SvgIcon :icon="item.icon" class="text-32px" />
            <CountTo
              :prefix="item.unit"
              :start-value="1"
              :end-value="item.value"
              class="text-30px text-white dark:text-dark"
            />
          </div>
        </GradientBg>
      </NGi>
    </NGrid>
  </NCard>
</template>

<style scoped></style>
